package com.ys.peony.car300;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.ys.peony.R;
import com.ys.peony.base.contract.BasicActivity;
import com.ys.peony.base.contract.CarPledgeContract;
import com.ys.peony.model.domain.CarDetailModel;
import com.ys.peony.model.domain.SaveCarModel;
import com.ys.peony.presenter.CarPledgePresenter;
import com.ys.peony.ui.activity.CarPledgeActivity;
import com.ys.peony.utils.ToastUtils;
import com.ys.peony.utils.YsUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import com.yklab.library.refreshlayout.adapters.CommonRecyclerViewAdapter;
import com.yklab.library.refreshlayout.holder.CommonRecyclerViewHolder;

public class BaoDanActivity extends BasicActivity<CarPledgeContract.iBaodanView, CarPledgePresenter> implements  View.OnClickListener, CarPledgeContract.iBaodanView {


    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    @BindView(R.id.moment_iv_edit)
    LinearLayout moment_iv_edit;
    @BindView(R.id.titleback)
    RelativeLayout titleback;

    private CommonRecyclerViewAdapter mAdapter;
    private List<CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean.VehicleSlipsBean> mList;
    private LinearLayoutManager mLayoutManager;

    @Override
    protected CarPledgePresenter initPresenter() {
        return new CarPledgePresenter();
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_bao_dan;
    }

    @Override
    protected void initView() {
        super.initView();
        initWidget();
        initListener();
        setAdapter();

    }

    @Override
    protected void onStart() {
        super.onStart();
        reFreshList(CarDataManager.getInstance().getCarDetailModel());
    }

    private void initWidget() {
        mList = new ArrayList();
        mLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
    }

    private void setAdapter() {
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
    }

    private void initListener() {
        moment_iv_edit.setOnClickListener(this);

        mAdapter = new CommonRecyclerViewAdapter<CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean.VehicleSlipsBean>(this, mList, R.layout.view_item_baodan) {
            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean.VehicleSlipsBean vehicleSlipsBean = mList.get(position);
                ((CommonRecyclerViewHolder) holder).setText(R.id.baoxian_content, vehicleSlipsBean.getInsurance_company_desr());
                switch (vehicleSlipsBean.getGuarantee_slip_type()) {
                    case "1":
                        ((CommonRecyclerViewHolder) holder).setImgResource(R.id.baoxian_type, R.mipmap.jqx_icon);
                        break;
                    case "2":
                        ((CommonRecyclerViewHolder) holder).setImgResource(R.id.baoxian_type, R.mipmap.syx_icon);
                        break;
                    case "3":
                        ((CommonRecyclerViewHolder) holder).setImgResource(R.id.baoxian_type, R.mipmap.car_tax_icon);
                        break;

                }
                CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean.VehicleSlipsBean.GuaranteeBeginBean guaranteeBeginBean = vehicleSlipsBean.getGuarantee_begin();
                CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean.VehicleSlipsBean.GuaranteeEndBean guaranteeEndBean = vehicleSlipsBean.getGuarantee_end();
                String time = "" + YsUtil.getDateToString(guaranteeBeginBean.getTime()) + " ~ " + YsUtil.getDateToString(guaranteeEndBean.getTime());
                ((CommonRecyclerViewHolder) holder).setText(R.id.baoxian_date, time);

            }
        };
        mAdapter.isHasFooter(false);
        titleback.setOnClickListener(this);
    }



    @Override
    public void onFail(String msg) {
        ToastUtils.showToast(msg);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.moment_iv_edit:
                SaveCarModel saveCarModel = CarDataManager.getInstance().getSaveCarModel();
                if (saveCarModel != null && saveCarModel.getMortgageList() != null && saveCarModel.getMortgageList().size() > 0) {
                    String vehicle_id = String.valueOf(saveCarModel.getVehicleList().get(0).getId());
                    if (!TextUtils.isEmpty(vehicle_id)) {
                        Bundle bundle = new Bundle();
                        bundle.putString(AddBaoDanActivity.PARAM_VEHICLE_ID,vehicle_id);
                        skipToTargetPage(AddBaoDanActivity.class, bundle);
                    }
                }
                break;
            case R.id.titleback:
                skipToTargetPage(CarPledgeActivity.class, null);
        }
    }


    @Override
    public void onSuccess(int code) {
    }

    private void reFreshList(CarDetailModel carDetailModel) {
        CarDetailModel.BusiMortgageVoBean busiMortgageVoBean = carDetailModel.getBusiMortgageVo();
        if (busiMortgageVoBean != null) {
            List<List<CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean>> lists = busiMortgageVoBean.getBusivehicleandslipvoList();
            if (lists != null && lists.size() > 0) {
                int carId = CarDataManager.getInstance().getSaveCarModel().getVehicleList().get(0).getId();
                for (int i = 0; i < lists.size(); i++) {
                    CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean busivehicleandslipvoListBeen = lists.get(i).get(0);
                    if (busivehicleandslipvoListBeen.getVehicle_code() == carId) {
                        List<CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean.VehicleSlipsBean> vehicleSlipsBeen = busivehicleandslipvoListBeen.getVehicleSlips();
                        mList.clear();
                        mList.addAll(vehicleSlipsBeen);
                        mAdapter.notifyDataSetChanged();
                        return;
                    }
                }
            }
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        //回退跳到主界面
        skipToTargetPage(CarPledgeActivity.class, null);
    }
}
